<?php

namespace app\home\controller;

use think\Db;

class Index extends Common
{

    public function index()
    {
        return $this->display('index/index');
    }

    public function search($mid = 0, $cid = 0, $keyword = '')
    {
        $result = $this->validate([
            'mid' => $mid,
            'cid' => $cid,
            'keyword' => $keyword,
        ], [
            'mid|模型' => 'require|number',
            'cid|栏目' => 'number',
            'keyword|标题关键词' => 'require|chsDash',
        ]);
        if (true !== $result) {
            $this->error($result);
        }
        $modellist = Db::name('model')->where('purpose', 'column')->column('id,title,table');
        if (empty($modellist)) {
            return $this->error('没有可搜索模型~');
        }
        if ($mid) {
            $modelTable = Db::name('model')->where('id', $mid)->where('purpose', 'column')->value('table');
            if (empty($modelTable)) {
                $this->error('模型错误~');
            }
            $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
            if (empty($searchField)) {
                $this->error('没有设置搜索字段~');
            }
            $where = '';
            foreach ($searchField as $vo) {
                $where .= "$vo like '%$keyword%' or ";
            }
            $where = '(' . substr($where, 0, -4) . ') ';
            $where .= " and status='1'";
            $list = model('ModelField')->getDataList($modelTable, $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
        } else {
            foreach ($modellist as $key => $vo) {
                $mid = $key;
                $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
                if (empty($searchField)) {
                    continue;
                }
                $where = '';
                foreach ($searchField as $v) {
                    $where .= "$v like '%$keyword%' or ";
                }
                $where = '(' . substr($where, 0, -4) . ') ';
                $where .= " and status='1'";
                $list = model('ModelField')->getDataList($vo['table'], $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
                if ($list->isEmpty()) {
                    continue;
                } else {
                    break;
                }
            }
        }
        $num = $list->count();
        $this->assign([
            'mid' => $mid,
            'cid' => $cid,
            'keyword' => $keyword,
            'num' => $num,
            'list' => $list->toArray(),
            'page' => $list->render(),
            'columnlist' => model('Column')->getColumn('sort', 'id,path,title,type,model_id'),
            'modellist' => $modellist,
        ]);
        return $this->display('index/search');
    }

    public function psearch($c = '0', $t1 = 0, $t2 = 0, $keyword = '')
    {
        $result = $this->validate([
            'keyword' => $keyword,
        ], [
            'keyword|标题关键词' => 'require|chsDash',
        ]);
        if (true !== $result) {
            $this->error($result);
        }

        $where = 'class=' . $c;
        if ($t2 != 0) {
            $where = $where . ' and typeid=' . $t2;
        }

        if ($t1 != 0) {
            $tlist = model('\app\member\model\Types')->where('path', '0,' . $t1 . ',')->column('id');
            $types = implode(",", $tlist);
            $where = $where . ' and (typeid=' . $t1 . ' or typeid in (' . $types . '))';
        }
        $plist = model('\app\member\model\Supply')->where($where)->where('title', 'like', '%' . $keyword . '%')->order('id desc')->paginate(12);

        return $this->display('index/gongqiu', ['c' => $c, 't1' => $t1, 't2' => $t2, 'plist' => $plist, 'keyword' => $keyword]);
    }

    public function chaxun($mid = 8, $cid = 31, $keyword = '要查询的标准名称或标准号')
    {

        $result = $this->validate([
            'mid' => $mid,
            'cid' => $cid,
            'keyword' => $keyword,
        ], [
            'mid|模型' => 'require|number',
            'cid|栏目' => 'number',
            'keyword|标题关键词' => 'require|chsDash',
        ]);
        if (true !== $result) {
            $this->error($result);
        }
        $modellist = Db::name('model')->where('purpose', 'column')->column('id,title,table');
        if (empty($modellist)) {
            return $this->error('没有可搜索模型~');
        }
        if ($mid) {
            $modelTable = Db::name('model')->where('id', $mid)->where('purpose', 'column')->value('table');
            if (empty($modelTable)) {
                $this->error('模型错误~');
            }
            $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
            if (empty($searchField)) {
                $this->error('没有设置搜索字段~');
            }
            $where = '';
            foreach ($searchField as $vo) {
                $where .= "$vo like '%$keyword%' or ";
            }
            $where = '(' . substr($where, 0, -4) . ') ';
            $where .= " and status='1'";
            $list = model('ModelField')->getDataList($modelTable, $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
        } else {
            foreach ($modellist as $key => $vo) {
                $mid = $key;
                $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
                if (empty($searchField)) {
                    continue;
                }
                $where = '';
                foreach ($searchField as $v) {
                    $where .= "$v like '%$keyword%' or ";
                }
                $where = '(' . substr($where, 0, -4) . ') ';
                $where .= " and status='1'";
                $list = model('ModelField')->getDataList($vo['table'], $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
                if ($list->isEmpty()) {
                    continue;
                } else {
                    break;
                }
            }
        }
        $num = $list->count();
        $this->assign([
            'mid' => $mid,
            'cid' => $cid,
            'keyword' => $keyword,
            'num' => $num,
            'list' => $list->toArray(),
            'page' => $list->render(),
            'columnlist' => model('Column')->getColumn('sort', 'id,path,title,type,model_id'),
            'modellist' => $modellist,
        ]);

        //dump($list);

        return $this->display('index/chaxun');
    }

    public function jindu($mid = 9, $cid = 48, $keyword = '请输入测试号')
    {

        $result = $this->validate([
            'mid' => $mid,
            'cid' => $cid,
            'keyword' => $keyword,
        ], [
            'mid|模型' => 'require|number',
            'cid|栏目' => 'number',
            'keyword|标题关键词' => 'require|chsDash',
        ]);
        if (true !== $result) {
            $this->error($result);
        }
        $modellist = Db::name('model')->where('purpose', 'column')->column('id,title,table');
        if (empty($modellist)) {
            return $this->error('没有可搜索模型~');
        }
        if ($mid) {
            $modelTable = Db::name('model')->where('id', $mid)->where('purpose', 'column')->value('table');
            if (empty($modelTable)) {
                $this->error('模型错误~');
            }
            $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
            if (empty($searchField)) {
                $this->error('没有设置搜索字段~');
            }
            $where = '';
            foreach ($searchField as $vo) {
                $where .= "$vo like '%$keyword%' or ";
            }
            $where = '(' . substr($where, 0, -4) . ') ';
            $where .= " and status='1'";
            $list = model('ModelField')->getDataList($modelTable, $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
        } else {
            foreach ($modellist as $key => $vo) {
                $mid = $key;
                $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
                if (empty($searchField)) {
                    continue;
                }
                $where = '';
                foreach ($searchField as $v) {
                    $where .= "$v like '%$keyword%' or ";
                }
                $where = '(' . substr($where, 0, -4) . ') ';
                $where .= " and status='1'";
                $list = model('ModelField')->getDataList($vo['table'], $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
                if ($list->isEmpty()) {
                    continue;
                } else {
                    break;
                }
            }
        }
        $num = $list->count();
        $this->assign([
            'mid' => $mid,
            'cid' => $cid,
            'keyword' => $keyword,
            'num' => $num,
            'list' => $list->toArray(),
            'page' => $list->render(),
            'columnlist' => model('Column')->getColumn('sort', 'id,path,title,type,model_id'),
            'modellist' => $modellist,
        ]);

        //dump($list);

        return $this->display('index/jindu');
    }

    public function authenticity($mid = 9, $cid = 48, $reportnum = '请输入报告号', $testnum = '请输入测试号', $client = '请输入委托单位全称')
    {

        $result = $this->validate([
            'mid' => $mid,
            'cid' => $cid,
            'reportnum' => $reportnum,
            'testnum' => $testnum,
            'client' => $client,
        ], [
            'mid|模型' => 'require|number',
            'cid|栏目' => 'number',
            'reportnum|报告号' => 'require',
            'testnum|测试号' => 'require',
            'client|委托单位' => 'require',
        ]);
        if (true !== $result) {
            $this->error($result);
        }
        $modellist = Db::name('model')->where('purpose', 'column')->column('id,title,table');
        if (empty($modellist)) {
            return $this->error('没有可搜索模型~');
        }
        if ($mid) {
            $modelTable = Db::name('model')->where('id', $mid)->where('purpose', 'column')->value('table');
            if (empty($modelTable)) {
                $this->error('模型错误~');
            }
            $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
            if (empty($searchField)) {
                $this->error('没有设置搜索字段~');
            }
            $where = '';
            // foreach ($searchField as $vo) {
            //     $where .= "$vo like '%$keyword%' or ";
            // }
            $where .= "reportnum = '$reportnum' and testnum = '$testnum' and client = '$client' ";
            $where = '(' . $where . ') ';
            $where .= " and status='1'";
            $list = model('ModelField')->getDataList($modelTable, $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
        } else {
            foreach ($modellist as $key => $vo) {
                $mid = $key;
                $searchField = Db::name('model_field')->where('model_id', $mid)->where('ifmain', 1)->where('ifsearch', 1)->column('name');
                if (empty($searchField)) {
                    continue;
                }
                $where = '';
                // foreach ($searchField as $v) {
                //     $where .= "$v like '%$keyword%' or ";
                // }
                // $where = '(' . substr($where, 0, -4) . ') ';
                $where .= "reportnum = '$reportnum' and testnum = '$testnum' and client = '$client' ";
                $where = '(' . $where . ') ';
                $where .= " and status='1'";
                $list = model('ModelField')->getDataList($vo['table'], $where, "*", "", "orders,id desc", "", [15, false, ['query' => ['mid' => $mid, 'cid' => $cid, 'keyword' => $keyword]]], $cid);
                if ($list->isEmpty()) {
                    continue;
                } else {
                    break;
                }
            }
        }
        $num = $list->count();
        $this->assign([
            'mid' => $mid,
            'cid' => $cid,
            'reportnum' => $reportnum,
            'testnum' => $testnum,
            'client' => $client,
            'num' => $num,
            'list' => $list->toArray(),
            'page' => $list->render(),
            'columnlist' => model('Column')->getColumn('sort', 'id,path,title,type,model_id'),
            'modellist' => $modellist,
        ]);

        //dump($list);
        return $this->display('index/authenticity');
    }

    /**
     * [gongqiu description]
     * @Author    momochong0
     * @DateTime  2019-05-20
     * @Copyright [copyright]
     * @param     string      $c  [供求分类]
     * @param     integer     $t1 [大类]
     * @param     integer     $t2 [小类]
     * @return    [type]          [description]
     */
    public function gongqiu($c = '0', $t1 = 0, $t2 = 0, $keyword = '')
    {
        $where = 'class=' . $c;
        if ($t2 != 0) {
            $where = $where . ' and typeid=' . $t2;
        }

        if ($t1 != 0) {
            $tlist = model('\app\member\model\Types')->where('path', '0,' . $t1 . ',')->column('id');
            $types = implode(",", $tlist);
            $where = $where . ' and (typeid=' . $t1 . ' or typeid in (' . $types . '))';
        }
        $where = $where . " and (title like '%" . $keyword . "%')";
        $plist = model('\app\member\model\Supply')->where($where)->order('id desc')->paginate(12);
        // $list->append(['\app\common\model\Attachment.path'])->toArray();
        // if (!empty($plist)) {
        //     foreach ($plist as &$vo) {
        //         if ($vo['pictures']) {
        //             $vo['path'] = model('\app\common\model\Attachment')->getFileInfo($vo['pictures']);
        //         }
        //     }
        // }
        // dump($plist);
        // exit();
        return $this->display('index/gongqiu', ['c' => $c, 't1' => $t1, 't2' => $t2, 'plist' => $plist, 'keyword' => $keyword]);
    }

    public function detail($id = 0)
    {
        $supply = model('\app\member\model\Supply')->where('id', $id)->find();

        $pic = model('\app\common\model\Attachment')->getFileInfo($supply['pictures'], 'path');
        // dump($pic);
        // exit();
        return $this->display('index/detail', ['supply' => $supply, 'pic' => $pic]);
    }

    public function indexzazhi()
    {
        return $this->display('index/indexzazhi');
    }
}
